 
package com.ybfenpan.servlet;


import com.ybfenpan.model.Member;
import com.ybfenpan.utils.SysConst;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.Calendar;


public class FilterMember  implements Filter {

	public final static String encoding = "UTF-8";
    
    private FilterConfig config = null;
    private String encode = null;
    
	public void init(FilterConfig config) throws ServletException {
		
		this.config = config;
        this.encode = config.getInitParameter("encoding");
        if(this.encode == null) {
            this.encode = encoding;
        }
	}
	 
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
                         FilterChain filterChain) throws IOException, ServletException {


		HttpServletRequest request=(HttpServletRequest)servletRequest;
		HttpServletResponse response=(HttpServletResponse)servletResponse;
		
		if (this.encode != null) {
        	
			request.setCharacterEncoding(this.encode);
        }  
		String uri = request.getRequestURI();
	    request.getQueryString();
	    String ch = URLDecoder.decode(uri, encode); 
	     
	    if(uri.equals(ch)) {
	    	HttpSession session=request.getSession();
	     	Member member = (Member)session.getAttribute(SysConst.MEMBER);
			
			if(member!=null){
				Calendar c=Calendar.getInstance();
				int shi = c.get(Calendar.HOUR_OF_DAY);		//时
				int fen = c.get(Calendar.MINUTE);			//分
				if(shi==23){
					if(fen>=55){
						//response.sendRedirect(request.getContextPath()+"/session-out-member.jsp");
						//response.sendRedirect(request.getContextPath()+"/loginMember.jsp");
					}
				}
				if(shi==0){
					if(fen<=20){
						//response.sendRedirect(request.getContextPath()+"/session-out-member.jsp");
						//response.sendRedirect(request.getContextPath()+"/loginMember.jsp");
					}
				}
				filterChain.doFilter(servletRequest, servletResponse);
			}else{
				
				response.sendRedirect(request.getContextPath()+"/session-out-member.jsp");
				
			}

	    }
		
		
		
		

	}
	
	public void destroy() {
		config = null;
	}
	
	
}